package chain

import (
	"fmt"
	"io"
)

type tLoggerFilter struct {
	writer io.StringWriter
	level LoggingLevel
	chain ILoggerFilter
}

func newLoggerFilter(writer io.StringWriter, level LoggingLevel, filter ILoggerFilter) ILoggerFilter {
	return &tLoggerFilter{
		writer, level, filter,
	}
}

func (me *tLoggerFilter) Next(filter ILoggerFilter) {
	me.chain = filter
}

func (me *tLoggerFilter) Handle(level LoggingLevel, msg string) {
	if me.level == level {
		_,_ = me.writer.WriteString(fmt.Sprintf("%v %s", me.level, msg))

	} else {
		if me.chain != nil {
			me.chain.Handle(level, msg)
		}
	}
}